package edu.geo.filters;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vividsolutions.jts.geom.Coordinate;
import edu.geo.containers.GeoJson;
import edu.geo.containers.Json;
import edu.geo.containers.geom.impl.LineSegment;
import edu.geo.containers.geom.impl.Point;
import edu.geo.containers.geomdomain.GeometricDomain;
import edu.geo.exception.GeoInfoException;
import edu.geo.protocol.Properties;
import edu.geo.service.IGeometricDomainServiceFacade;
import edu.geo.spaghetti.SpaghettiRepresentation;

public class SpaghettiProcess extends GeoProcessDecorator 
{

	private static final Log LOGGER = LogFactory.getLog(SpaghettiProcess.class);
	private IGeometricDomainServiceFacade geomDomainService;
	
	/*
	 * Ejemplo de petición de recuperacion  de elemntos en modelo spaguetti
	{
	    "type": "Operation",
	    "name": "retrieveSpaghettiFeatures",
	    "properties": {
	        "user": "geoinfouser",
	        "domainId": "10" 
	    }
	}
	*/
	
	public SpaghettiProcess(IGeometricDomainServiceFacade geomDomainService) {
		super();
		this.geomDomainService = geomDomainService;
	}
	

	@Override
	public Json dispatchResult(Json requestParams) {
		String domainIdStr = requestParams.getProperties().getDomainId();
		int domainId = Integer.parseInt(domainIdStr);
		LOGGER.info("retrieve spaghetty features to domain:"+domainId);
		 Json jsonFeatures= new Json(); 
		try {		
		GeometricDomain domain;
		domain = this.geomDomainService.retrieve(domainId);
		SpaghettiRepresentation spaghettiRep = this.geomDomainService.asSpaghettiRepresentation(domain);
		List<edu.geo.containers.geom.impl.Polygon> polygons = spaghettiRep.getPolygons();
		//SpaghettiRepresentation spRepresentation = this.geomDomainService.asSpaghettiRepresentation(domain);
		List<LineSegment> lines = spaghettiRep.getLineSegments();
		List<Point> points = spaghettiRep.getPoints();		 
		 jsonFeatures.setName("spaghettiFeaturesData");
			jsonFeatures.setType("FeatureCollection");
			ArrayList<Json> featureList= new ArrayList<Json>();
		    
			if(polygons!=null)
		     
		      {
		    	  for(edu.geo.containers.geom.impl.Polygon polygon:polygons)
		    	  {
		    		  Json feature= new Json();
					  feature.setType("Feature");
						GeoJson geoJson= new GeoJson();
						geoJson.setType("Polygon");
						
						ArrayList<ArrayList<ArrayList<Double>>> coordinates = new ArrayList<ArrayList<ArrayList<Double>>>();
						ArrayList<ArrayList<Double>> arraycoords = new ArrayList<ArrayList<Double>>();
						
						List<Point> coords = polygon.getPoints();
						for(Point point:coords)
						{
							
							ArrayList<Double> coord= new ArrayList<Double>();
							coord.add(point.getX());
							coord.add(point.getY());
							System.out.println("ds"+point.getX()+" "+point.getY());
							arraycoords.add(coord);
						}
						coordinates.add(arraycoords);
						geoJson.setCoordinates(coordinates);
						feature.setGeometry(geoJson);
						Properties prp = new Properties();
						prp.setId(""+polygon.getStrId());
						prp.setResult(""+polygon.asSpaghettiRepresentation());
						feature.setProperties(prp);
						featureList.add(feature);
		    	  }
		    	  
		      }
			
			 if(lines!=null)
		      {
		    	  for(LineSegment line:lines)
		    	  {
		    		  Json feature= new Json();
					  feature.setType("Feature");
						GeoJson geoJson= new GeoJson();
						geoJson.setType("LineString");
						ArrayList<ArrayList<Double>> coordinates = new ArrayList<ArrayList<Double>>();
						Coordinate[] coords = line.getCoordinates();
						int numCoords=coords.length;
						for(int i =0;i<numCoords;i++)
						{
							ArrayList<Double> coord= new ArrayList<Double>();
							coord.add(coords[i].x);
							coord.add(coords[i].y);
							coordinates.add(coord);
						}
						geoJson.setCoordinates(coordinates);
						feature.setGeometry(geoJson);
						Properties prp = new Properties();
						prp.setId(""+line.getStrId());
						prp.setResult(""+line.asSpaghettiRepresentation());
						feature.setProperties(prp);
						featureList.add(feature);			
		    	  }
		    	  
		      }
			 if(points!=null)
		      {
		    	  for(Point point:points)
		    	  {
		    		 Json feature = new Json();
		    		 feature.setType("Feature");
		    		 GeoJson geoJson= new GeoJson();
					 geoJson.setType("Point");
					 ArrayList<Double> coordinates = new ArrayList<Double>();
					 coordinates.add(point.getX());
					 coordinates.add(point.getY());
					 geoJson.setCoordinates(coordinates);
					 feature.setGeometry(geoJson);
					 Properties prp = new Properties();
					 prp.setId(point.getStrId()+"");
					 prp.setResult(""+point.asSpaghettiRepresentation());
					 feature.setProperties(prp);
					 featureList.add(feature);				
		    	  }

		      }

		      jsonFeatures.setFeatures(featureList);
		 
		 
		} catch (GeoInfoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonFeatures;
	}
}
